
export default class MyTools {

    static banner: wx.BannerAd;
    static grid: wx.GridAd;
    static Zero: Laya.Vector3 = new Laya.Vector3(0, 0, 0);

    /**
     * 添加子物体
     * @param child 子物体
     * @param parent 父物体
     */
    static WorldAddChild(child, parent) {
        let pos = child.transform.position;
        parent.addChild(child);
        child.transform.position = pos;
    }

    /**
      * 向量乘一个数
      * @param 向量 
      * @param 系数 
      */
    static Vertor3dCheng(vec: Laya.Vector3, num: number) {
        return new Laya.Vector3(vec.x * num, vec.y * num, vec.z * num);
    }
    /**
   * 找到parent一级下边所有name的物体
   * @param 父物体 
   * @param 需要寻找的名字 
   * @returns 返回这个名字的数组
   */
    static GetOneKindAllObj(parent: Laya.Sprite3D, name: string) {
        let obj: Array<Laya.Sprite3D> = [];
        for (let index = 0; index < parent.numChildren; index++) {
            if (parent.getChildAt(index).name == name) {
                obj.push(parent.getChildAt(index) as Laya.Sprite3D);
            }
        }
        return obj;
    }
    /**从一个父节点，一直找到name的物体  无论层级
* @param 父物体 
* @param 需要寻找的名字 
* @returns 返回这个名字的第一个物体
*/
    static GetChildFromParent(parent: Laya.Sprite3D, name: string) {
        let getobj = parent.getChildByName(name) as Laya.Sprite3D;
        if (getobj != null) {
            return getobj;
        }
        for (let index = 0; index < parent.numChildren; index++) {
            let temp = parent.getChildAt(index) as Laya.Sprite3D
            getobj = this.GetChildFromParent(temp, name)
            if (getobj != null) {
                return getobj;
            }
        }
        return null;
    }
    /**冒泡  返回 （导出）*/
    static maopao(ary: Array<any>) {
        if (ary == null) {
            return;
        }
        var temparray: Array<any> = [];
        temparray = MyTools.getRandomArr(ary);
        for (let i = 0; i < temparray.length - 1; i++) {
            for (let j = 0; j < temparray.length - 1; j++) {
                if (temparray[j].sort > temparray[j + 1].sort) {
                    var temp;
                    temp = temparray[j];
                    temparray[j] = temparray[j + 1]
                    temparray[j + 1] = temp;
                }
            }
        }
        return temparray;
    }
    /**随机打乱数组*/
    static getRandomArr(arr) {
        let i = arr.length;
        while (i) {
            let j = Math.floor(Math.random() * i--);
            [arr[j], arr[i]] = [arr[i], arr[j]];
        }
        return arr;
    }
    /**随机范围整数 [] */
    static getRandomNumInt(min: number, max: number) {
        var r = Math.random();
        var rr = r * (max - min + 1) + min;
        return Math.floor(rr);
    }
    /**随机范围小数 [] */
    static getRandomNumFloat(min: number, max: number) {
        var r = Math.random();
        var rr = r * (max - min + 1) + min;
        rr = parseFloat(rr.toFixed(2));
        return rr;
    }
    /**[SixGod]
     * 向量投影长度, 向量CA 在向量 CB 上的投影长度
     * @param {Laya.Vector3} A
     * @param {Laya.Vector3} C
     * @param {Laya.Vector3} B
     */
    static ProjectDistance(A, C, B) {
        var CA = new Laya.Vector3();
        Laya.Vector3.subtract(A, C, CA);
        var angle = this.Angle2(CA, B) * Math.PI / 180;
        var distance = Laya.Vector3.distance(A, C);
        distance *= Math.cos(angle);
        return distance;
    }

    /**[SixGod]
     * 向量夹角
     * @param {Laya.Vector3} ma 向量A
     * @param {Laya.Vector3} mb 向量B
     */
    static Angle2(ma, mb) {
        var v1 = (ma.x * mb.x) + (ma.y * mb.y) + (ma.z * mb.z);
        var ma_val = Math.sqrt(ma.x * ma.x + ma.y * ma.y + ma.z * ma.z);
        var mb_val = Math.sqrt(mb.x * mb.x + mb.y * mb.y + mb.z * mb.z);
        var cosM = v1 / (ma_val * mb_val);
        if (cosM < -1) cosM = -1;
        if (cosM > 1) cosM = 1;

        var angleAMB = Math.acos(cosM) * 180 / Math.PI;
        return angleAMB;
    }
    /**百分比判断 */
    static getRandomPer(per) {
        if (!per) return false;
        return (per >= (Math.random() * 100))
    }
    /**改变背景颜色 */
    static ChangeBGColor(Image: Laya.Image, color) {
        var colorFilter = new Laya.ColorFilter();
        colorFilter.setColor(color);
        Image.filters = [colorFilter];
    }
    /**去掉空白字符 */
    static strim(str: string) {
        const reg = /\s+/g;
        return str.replace(reg, '');
    }
    /**随机取在玩人数 */
    static  GetPlayNum(index,len) {
        let a = Math.floor((999 - 200) / len);
        let r = 200 + (len - index) * a + this.getRandomNumInt(0, a);
        let str = r + "万人在玩"
        return str;
    }
}